import { baseURL } from './../../utils/api/http';
import { defineStore } from "pinia";
import { dictlist } from "../../utils/api/dict/dict";// 字典查询
import {
  getLossList,// 报损物资数据
  deleteLoss,// 删除报损物资
  deleteAllLoss,// 删除报损物资
  getSuppliesList,// 查询仓库物资分页列表
  saveDamage,// 新增报损单
  submitDamage,// 报损单提交
  modifyDamage,// 报损单修改
} from "../../utils/api/lossManage/lossManage";
// import { useMemberStore } from "@/stores/modules/member";
const quantityTitle = [
  { text: '物资编号', style: 'width:80px;', name: 'GoodsCode' },
  { text: '物资名称', style: 'width:120px;', name: 'GoodsName' },
  { text: '报损类型', style: 'width:70px;', name: 'DamageTypeName' },
  { text: '报损日期', style: 'width:172px;', name: 'DamageDate' },
  { text: '报损状态', style: 'width:70px;', name: 'DamageStatusName' },
  { text: '操作人', style: 'width:70px;', name: 'DamageOperatorName' },
  { text: '审批状态', style: 'width:80px;', name: 'DamageReplyStatusName' },
]// 报损管理表格标题

const lossAddTitle = [
  { text: '物资编号', style: 'width:130px;', name: 'GoodsCode' },
  { text: '物资名称', style: 'width:180px;', name: 'GoodsName' },
  { text: '批次/有效期', style: 'width:130px;', name: 'GoodsBatch' },// GoodsBatch 批次 ExpirationDate 有效期
  { text: '单位', style: 'width:70px;', name: 'Units' },
  { text: '库存数量', style: 'width:60px;', name: 'GoodsNo' }
]// 报损新增

const lossPopupTitle = [
  { text: '物资名称', style: 'width:100px;', name: 'GoodsName' },
  { text: '物资类型', style: 'width:60px;', name: 'GoodsTypeName' },
  { text: '批次/有效期', style: 'width:130px;', name: 'GoodsBatch' },// GoodsBatch 批次 ExpirationDate 有效期
  { text: '数量/单位', style: 'width:90px;', name: 'GoodsNumber' },
  { text: '库位', style: 'width:160px;', name: 'BoxName' }
]// 报损新增弹窗

export const useLossManageStore = defineStore(
  "lossManage",
  () => {
    const titleStatus = quantityTitle;// 报损管理表格标题
    const lossAddStatus = lossAddTitle;// 报损新增
    const lossPopupStatus = lossPopupTitle;// 报损新增

    let lossTypeDict = ref([])// 报损类型字典
    let lossStatusDict = ref([])// 报损状态字典
    let total = ref(0)// 数据总条数
    let dataList = ref([])// 报损管理数据内容
    let lossAddList: any = ref([])// 报损新增数据内容

    const getDictData = async (data: object, mode: string) => {
      let res = await dictlist(data)
      let lossDict = res.data.map((item: AnyObject) => { return { text: item.DictName, value: item.DictCode } })
      switch (mode) {
        case 'lossTypeDict':
          lossTypeDict.value = lossDict
          break;

        case 'lossStatusDict':
          lossStatusDict.value = lossDict
          break;
        default:
          break;
      }
    }// 字典查询

    const getLossListData = async (data: object) => {
      let res = await getLossList(data)
      total.value = res.data.totalCount
      dataList.value = res.data.list
    }// 获取报损管理数据

    const deleteLossData = async (data: object) => {
      try {
        await deleteLoss(data)
        uni.showToast({
          title: '删除成功！',
          duration: 1000
        });
      } catch (error: any) {
        uni.showToast({
          title: error,
          icon: 'error',
          duration: 2000
        });
      }
    }// 删除报损管理数据

    const deleteLossAllData = async (data: object) => {
      try {
        await deleteAllLoss(data)
        uni.showToast({
          title: '删除成功！',
          duration: 1000
        });
      } catch (error: any) {
        uni.showToast({
          title: error,
          icon: 'error',
          duration: 2000
        });
      }
    }// 批量删除报损管理数据

    const getSupplies = getSuppliesList// 查询仓库物资分页列表

    const lossSumbit = async (data: object, mode: string) => {
      uni.showLoading({
        title: mode === 'save' ? '正在保存' : '正在提交'
      });
      try {
        let res: any = await saveDamage(data)
        if (mode === 'sumbit') {
          console.log(res);
          console.log('sumbit');
          await submitDamage({ ...data, Uuid: res.Uuid })
        }
        uni.hideLoading()
        uni.showToast({
          title: mode === 'save' ? '保存成功' : '提交成功',
          icon: "success",
          duration: 1500
        });
        setTimeout(() => {
          uni.navigateBack()
        }, 1500);// 返回上一页
      } catch (error) {
        uni.hideLoading()
        uni.showToast({
          title: mode === 'save' ? '保存失败' : '提交失败',
          icon: "error",
          duration: 1500
        });
      }
    }// 保存或提交报损单

    const modifySumbit = async (data: object, mode: string) => {
      uni.showLoading({
        title: mode === 'save' ? '正在保存' : '正在提交'
      });
      try {
        if (mode === 'save') {
          await modifyDamage(data)
        }
        if (mode === 'sumbit') {
          await submitDamage(data)
        }
        uni.hideLoading()
        uni.showToast({
          title: mode === 'save' ? '保存成功' : '提交成功',
          icon: "success",
          duration: 1500
        });
        setTimeout(() => {
          uni.navigateBack()
        }, 1500);// 返回上一页
      } catch (error) {
        uni.hideLoading()
        uni.showToast({
          title: mode === 'save' ? '保存失败' : '提交失败',
          icon: "error",
          duration: 1500
        });
      }
    }// 修改或提交报损单

    return {
      titleStatus,
      lossAddStatus,
      lossPopupStatus,
      lossTypeDict,
      lossStatusDict,
      total,
      dataList,
      lossAddList,
      getDictData,
      getLossListData,
      deleteLossData,
      deleteLossAllData,
      getSupplies,
      lossSumbit,
      modifySumbit
    };
  },
  // TODO: 持久化
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ['lossTypeDict', 'lossStatusDict'],
    },
  }
);
